home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / files / tcpip / amiga / asrc29p.lha / rspf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-29  |  55.1 KB  |  1,849 lines

  1. /*             RSPF - Radio Shortest Path First
  2.  * This code may be used for non-commercial purposes only.
  3.  *            Anders Klemets SM0RGV
  4.  * 890918 - Version 2.0
  5.  * 900816 - Version 2.1
  6.  *
  7.  */
  8. #include "global.h"
  9. #include "config.h"
  10. #include "mbuf.h"
  11. #include "proc.h"
  12. #include "timer.h"
  13. #include "netuser.h"
  14. #include "internet.h"
  15. #include "ip.h"
  16. #include "iface.h"
  17. #include "ax25.h"
  18. #include "arp.h"
  19. #include "icmp.h"
  20. #include "socket.h"
  21. #include "rspf.h"
  22.  
  23. extern struct route *rt_lookup __ARGS((int32 target));
  24.  
  25. #ifdef    AX25
  26. extern struct lq *al_lookup __ARGS((struct iface *ifp,char *addr,int sort));
  27. #else
  28. static struct lq *al_lookup __ARGS((struct iface *ifp,char *addr));
  29. #endif    /* AX25 */
  30.  
  31. struct rspf_stat Rspf_stat;
  32. struct rspfreasm *Rspfreasmq = NULLRREASM;
  33. struct rspfiface *Rspfifaces = NULLRIFACE;
  34. struct rspfadj *Adjs = NULLADJ;
  35. struct rspfrouter *Rspfrouters = NULLRROUTER;
  36. struct mbuf *Rspfinq = NULLBUF;
  37. struct timer Rspfreasmt, Susptimer;
  38. char *Rrh_message = NULLCHAR;
  39. unsigned short Rspfpingmax = 5;
  40. static int Keeprouter = 0;
  41. static int16 Envelopeid = 0;
  42. static int Nrspfproc = 0;
  43. static unsigned char Rspfsubseq = 0;
  44. static short Rspfseq = -32768 + 1;
  45. static char *findlowroute __ARGS((int32 addr,int *low,int add,int32 *resaddr,int *resbits));
  46. static void makeroutes __ARGS((void));
  47. static void partialupdate __ARGS((struct rspfrouter *rr,struct mbuf *bp));
  48. static struct rspfrouter *rr_lookup __ARGS((int32 addr));
  49. static void rr_delete __ARGS((int32 addr));
  50. static struct rspfiface *rtif_lookup __ARGS((int32 addr));
  51. static void rspfcsum __ARGS((struct mbuf **bpp,int32 dest));
  52. static void update_in __ARGS((struct mbuf *bp,int32 addr));
  53. static void node_in __ARGS((struct mbuf *bp,int32 addr,int full));
  54. static void sendonerspf __ARGS((int32 addr,int32 dest));
  55. static void sendtoall __ARGS((struct mbuf *bp,int nodecnt,struct rspfiface *riface));
  56. static int sendupdate __ARGS((struct mbuf *bp,int nodecnt,int32 addr));
  57. static int isnewer __ARGS((short a,short b));
  58. static void del_reasm __ARGS((struct rspfreasm *re));
  59. static void reasmtimeout __ARGS((void *t));
  60. static struct mbuf *rspfreasm __ARGS((struct mbuf *bp,struct rspfpacketh *rph,int32 addr));
  61. static struct mbuf *fragmenter __ARGS((struct mbuf *bp,int nodes,int16 mtu));
  62. static struct mbuf *makeadjupdate __ARGS((int32 addr,struct rspfrouter *rr));
  63. static void pushadj __ARGS((struct mbuf **bpp,int32 addr,int bits));
  64. static void sendrrh __ARGS((struct rspfiface *ri));
  65. static void sendrspf __ARGS((void));
  66. static void goodbadnews __ARGS((struct rspfadj *adj));
  67. static void add_rspfadj __ARGS((struct rspfadj *adj));
  68. static void rspfcheck __ARGS((struct rspfadj *adj));
  69. static void rspfping __ARGS((int oldstate,void *p,void *v));
  70.  
  71. /* IP passes its RSPF packets to this function */
  72. void
  73. rspf_input(iface,ip,bp,rxbroadcast)
  74. struct iface *iface;
  75. struct ip *ip;
  76. struct mbuf *bp;
  77. int rxbroadcast;
  78. {
  79.     struct pseudo_header ph;    /* Pseudo-header for checksumming */
  80.     if(bp == NULLBUF)
  81.         return;
  82.     if(Rspfifaces == NULLRIFACE){    /* Rspf main loop is not running */
  83.         free_p(bp);
  84.         return;
  85.     }
  86.     ph.length = ip->length - IPLEN - ip->optlen;
  87.     ph.source = ip->source;
  88.     ph.dest = ip->dest;
  89.     ph.protocol = ip->protocol;
  90.     if(cksum(&ph,bp,ph.length) != 0){
  91.         /* Checksum failed, ignore packet completely */
  92.         free_p(bp);
  93.         ++Rspf_stat.badcsum; 
  94.         return;
  95.     }
  96.     bp = pushdown(bp,1 + sizeof(ip->source) + sizeof(iface));
  97.     *bp->data = RSPFE_PACKET;
  98.     memcpy(bp->data + 1,&ip->source,sizeof(ip->source));
  99.     memcpy(bp->data + 1 + sizeof(ip->source),&iface,sizeof(iface));
  100.     enqueue(&Rspfinq,bp);
  101. }
  102.  
  103. /* Main loop in RSPF process */
  104. void
  105. rspfmain(v,v1,v2)
  106. int v;
  107. void *v1,*v2;
  108. {
  109.     union rspf rspf;        /* Internal packet structure */
  110.     struct rspfadj *adj;        /* Adjacency */
  111.     struct mbuf *bp, *tbp;
  112.     struct rspfiface *riface;
  113.     struct iface *iface;
  114.     struct route *rp;
  115.     int32 source;            /* Source address */
  116.     int cmd;
  117.     
  118.     for(;;) {
  119.          if(Rspfinq == NULLBUF)
  120.           pwait(&Rspfinq);
  121.          bp = dequeue(&Rspfinq);    /* at least 5 bytes */
  122.          cmd = PULLCHAR(&bp);    /* get internal event indicator */
  123.          pullup(&bp,(char *)&source,sizeof(source));
  124.          switch(cmd) {
  125.          case RSPFE_RRH:
  126.           sendrrh((struct rspfiface *)source);
  127.           break;
  128.          case RSPFE_CHECK:
  129.           rspfcheck((struct rspfadj *)source);
  130.           break;
  131.          case RSPFE_UPDATE:
  132.           sendrspf();
  133.           break;
  134.          case RSPFE_ARP:
  135.           adj = (struct rspfadj *) source;
  136.           source = adj->addr;        /* fall through */
  137.          case RSPFE_PACKET:
  138.           pullup(&bp,(char *)&iface,sizeof(iface));
  139.           break;
  140.          }
  141.          if(cmd != RSPFE_PACKET && cmd != RSPFE_ARP)
  142.           continue;
  143.          if(cmd == RSPFE_PACKET && ntohrspf(&rspf,&bp) == -1){
  144.           free_p(bp);
  145.           continue;
  146.          }
  147.          if(iface != NULLIF) {
  148.           for(riface = Rspfifaces; riface != NULLRIFACE; riface =
  149.               riface->next)
  150.                if(riface->iface == iface)
  151.                 break;
  152.          }
  153.          else
  154.           /* fails if there is no route or no RSPF interface */
  155.           riface = rtif_lookup(source);
  156.          if(riface == NULLRIFACE) {
  157.           free_p(bp);
  158.           if(cmd == RSPFE_PACKET)
  159.                ++Rspf_stat.norspfiface;
  160.           continue;    /* We do not use RSPF on this interface */
  161.          }
  162.          /* If we dont have an entry in the routing table for this station,
  163.           * or if the entry is less than 32 bits specific or has a higher
  164.           * cost our new route, and is pointing to the wrong interface,
  165.           * then add a correct, private, route.
  166.           */
  167.          rp = rt_lookup(source);
  168.          if(rp == NULLROUTE || (rp != NULLROUTE && rp->iface !=
  169.             riface->iface && (rp->bits < 32 || rp->metric >
  170.                       riface->quality)))
  171.           rt_add(source,32,0,iface,riface->quality,0,1);
  172.  
  173.          if(cmd == RSPFE_ARP) {
  174.           adj->cost = riface->quality;    /* default cost */
  175.           add_rspfadj(adj);
  176.           continue;
  177.          }
  178.          switch(rspf.hdr.type){        /* packet types */
  179.          case RSPF_RRH:
  180.           ++Rspf_stat.rrhin;
  181.           free_p(bp);
  182.           adj = (struct rspfadj *)callocw(1,sizeof(struct rspfadj));
  183.           adj->addr = rspf.rrh.addr;
  184.           adj->seq = rspf.rrh.seq;
  185.           adj->cost = riface->quality;    /* Default cost */
  186.           adj->state = RSPF_TENTATIVE;
  187.           if(rspf.rrh.flags & RSPFMODE)
  188.                adj->tos = DELAY;
  189.           else
  190.                adj->tos = RELIABILITY;
  191.           add_rspfadj(adj);
  192.           break;
  193.          case RSPF_FULLPKT:
  194.           ++Rspf_stat.updatein;
  195.           /* Feed the packet through the reassembly queue */
  196.           if((tbp = rspfreasm(bp,&rspf.pkthdr,source)) != NULLBUF)
  197.                update_in(tbp,source);
  198.           break;
  199.          }
  200.     }
  201. }
  202.  
  203. /* This function is called every time an RRH should be broadcasted.
  204.  * An RRH is sent on the interface given by ri or on every RSPF interface.
  205.  * The broadcast address of the interface must be routed onto the same
  206.  * interface (using a static route for instance) otherwise there will be no
  207.  * RRH sent on that interface.
  208.  */
  209. static void
  210. sendrrh(ri)
  211. struct rspfiface *ri;        /* interface to use, if specified */
  212. {
  213.     struct pseudo_header ph;
  214.     struct mbuf *bp, *data = NULLBUF;
  215.     struct rspfiface *riface;
  216.     struct route *rp;
  217.     struct rrh rrh;
  218.     for(riface = Rspfifaces; riface != NULLRIFACE; riface = riface->next) {
  219.      if(ri != NULLRIFACE && riface != ri)
  220.           continue;
  221.      if((rp = rt_lookup(riface->iface->broadcast)) != NULLROUTE &&
  222.         rp->iface == riface->iface){
  223.           rrh.version = RSPF_VERSION;
  224.           rrh.addr = riface->iface->addr;
  225.           ph.length = 0;
  226.           if(Rrh_message != NULLCHAR) {
  227.            data = alloc_mbuf(strlen(Rrh_message));
  228.            memcpy(data->data,Rrh_message,strlen(Rrh_message));
  229.            ph.length = data->cnt = strlen(Rrh_message);
  230.           }
  231.           ph.length += RRHLEN;
  232.           ph.source = riface->iface->addr;
  233.           ph.dest = riface->iface->broadcast;
  234.           ph.protocol = RSPF_PTCL;
  235.           /* Start the RRH link level packet counter at 1, since adj->seq
  236.            * is 0 only for ARP generated adjacencies. This is also correct
  237.            * since rawsndcnt will increase by one when the RRH is sent.
  238.            */
  239.           rrh.seq = riface->iface->rawsndcnt + 1;
  240.           /* Default is to use the same mode as the interface */
  241.           if(Rspfownmode == -1)
  242.            rrh.flags = !(riface->iface->flags & CONNECT_MODE);
  243.           else
  244.            rrh.flags = !(Rspfownmode & CONNECT_MODE);
  245.  
  246.           bp = htonrrh(&rrh,data,&ph);
  247.           ++Rspf_stat.rrhout;
  248.           ip_send(riface->iface->addr,riface->iface->broadcast,RSPF_PTCL,
  249.               0,2,bp,0,0,0); /* the ttl will be decremented to 1 */
  250.      }
  251.     }
  252. }
  253.  
  254. /* This function is called whenever an RRH packet has been received or when
  255.  * a new entry is added to the ARP table.
  256.  */
  257. static void
  258. add_rspfadj(adj)
  259. struct rspfadj *adj;
  260. {
  261.     struct rspfadj *oldadj, *tmp;
  262.     struct rspfiface *riface;
  263.     struct arp_tab *atp;
  264.     struct lq *alp;
  265.     int dif, origdif;
  266.     /* Find the previous copy of the adjacency, if there was any */
  267.     /* Insert the new adjacency */
  268.     adj->next = Adjs;
  269.     Adjs = adj;
  270.     for(oldadj = Adjs; oldadj->next != NULLADJ; oldadj = oldadj->next)
  271.     if(oldadj->next->addr == adj->addr) {
  272.         tmp = oldadj->next;
  273.         oldadj->next = oldadj->next->next;
  274.         oldadj = tmp;
  275.         break;
  276.     }
  277.     if(oldadj->addr != adj->addr || oldadj == adj)
  278.     oldadj = NULLADJ;
  279.     /* ARP entries give no sequence number, so save the old one */
  280.     if(oldadj != NULLADJ) {
  281.      if(adj->seq == 0)
  282.           adj->seq = oldadj->seq;
  283.      if(adj->tos == 0)
  284.           adj->tos = oldadj->tos;    /* they give no TOS either */
  285.     }
  286.     switch(adj->state) {
  287.     case RSPF_TENTATIVE:
  288.     if(oldadj != NULLADJ) {
  289.          /* If the adjacency was in OK state, it will remain there.
  290.           * An RRH or ARP upcall can never generate an OK -> Tentative
  291.           * transition.
  292.           */
  293.          if(oldadj->state == RSPF_OK)
  294.          adj->state = RSPF_OK;
  295.          adj->okcnt = oldadj->okcnt;
  296.          /* If old state was Bad, don't announce this adjacency until
  297.           * it is known to be OK, to prevent announcing an adjacency
  298.           * with an state transition loop such as OK -> Suspect -> Bad ->
  299.           * Tentative -> Suspect -> Bad -> Tentative -> ... 
  300.           */
  301.          if(oldadj->state == RSPF_BAD) {
  302.           adj->okcnt = 0;
  303.           stop_timer(&oldadj->timer);
  304.           /* Enter Suspect state at once, there is no point in
  305.            * dwelling as Tentative.
  306.            */
  307.           rspfcheck(adj);
  308.          }
  309.          else {
  310.           /* Inherit the old timer */
  311.           adj->timer.func = rspfevent;
  312.           adj->timer.arg = (void *) &adj->timer;
  313.           /* continue where the old timer is stopped */
  314.           adj->timer.start = read_timer(&oldadj->timer);
  315.           stop_timer(&oldadj->timer);
  316.           /* Set the timer to Suspectimer in the unlikely event that
  317.            * the timer was stopped and oldadj is not Suspect or Bad.
  318.            * Suspect state is dealt with further below.
  319.            */
  320.           if(adj->timer.start == 0)
  321.                adj->timer.start = dur_timer(&Susptimer);
  322.           start_timer(&adj->timer);
  323.           adj->timer.start = oldadj->timer.start;
  324.         }
  325.         /* We must now try to figure out from which AX.25 callsign this
  326.          * packet came. Looking at the ARP table may sometimes help. 
  327.          */
  328.         if(oldadj->seq != 0 && adj->seq != 0 &&
  329.            (riface = rtif_lookup(adj->addr)) != NULLRIFACE &&
  330.            (atp = arp_lookup(ARP_AX25,adj->addr)) != NULLARP &&
  331.            atp->state == ARP_VALID &&
  332.            (alp = al_lookup(riface->iface,atp->hw_addr,0)) != NULLLQ){
  333.         /* The wrapping of the modulus is taken into account here */
  334.         if(oldadj->seq > (MAXINT16 - 100) && adj->seq < 100)
  335.             origdif = adj->seq + MAXINT16 - oldadj->seq;
  336.         else
  337.             origdif = adj->seq - oldadj->seq;
  338.         if((alp->currxcnt - adj->heard) > (MAXINT16 - 100)
  339.            && adj->seq < 100)
  340.             dif = origdif + MAXINT16 - (alp->currxcnt - adj->heard);
  341.         else
  342.             dif = origdif - (alp->currxcnt - adj->heard);
  343.         adj->heard = alp->currxcnt;    /* Update the counter */
  344.         /* At this point, "origdif" equals the difference in sequence
  345.          * numbers between the two latest RRH packets, i.e. the
  346.          * number of AX.25 frames the station has sent during since
  347.          * the last RRH packet we heard. "dif" equals the number of
  348.          * AX.25 frames that we actually heard from that station
  349.          * during the same period.
  350.          */
  351.         if(origdif > 0 && dif > 0)
  352.             /* This algorithm can be improved, see 2.1 spec */
  353.             adj->cost = riface->quality*2-riface->quality*dif/origdif;
  354.         }
  355.         /* Ignore any new RRH's if a pinger process is already running */
  356.         if(oldadj->state == RSPF_SUSPECT) {
  357.          if(adj->heard != 0)        /* save new heard count */
  358.               oldadj->heard = adj->heard;
  359.          oldadj->next = adj->next;    /* adj is at start of chain */
  360.          Adjs = oldadj;
  361.          stop_timer(&adj->timer);
  362.          free((char *)adj);
  363.          return;
  364.         }
  365.         }
  366.     else {                    /* oldadj == NULLADJ */
  367.         rspfcheck(adj);            /* enter Suspect state */
  368.         /* A new adjacency may affect the routing table even though no
  369.          * routing updates have yet been received from it.
  370.          */
  371.         makeroutes();
  372.     }
  373.     break;
  374.     case RSPF_OK:
  375.     if(oldadj != NULLADJ) {
  376.         if(oldadj->state == RSPF_SUSPECT)
  377.         killproc(oldadj->pinger);
  378.         adj->okcnt += oldadj->okcnt;
  379.         stop_timer(&oldadj->timer);
  380.     }
  381.     else
  382.          makeroutes();            /* routing table may change */
  383.     if(adj->okcnt == 1)            /* A previously unkown route */
  384.         goodbadnews(adj);            /* ..that is good news */
  385.     break;
  386.     }
  387.     free((char *)oldadj);
  388. }
  389.  
  390. /* Take appropriate action if an adjacency is Bad or Tentative */
  391. static void
  392. rspfcheck(adj)
  393. struct rspfadj *adj;
  394. {
  395.     struct rspfadj *prev;
  396.     for(prev = Adjs; prev != NULLADJ; prev = prev->next)
  397.      if(prev->next == adj)
  398.           break;
  399.     switch(adj->state){
  400.     case RSPF_OK:
  401.      adj->state = RSPF_TENTATIVE;     /* note fall-thru */
  402.     case RSPF_TENTATIVE:
  403.      if (Nrspfproc < RSPF_PROCMAX) {
  404.           Nrspfproc++;
  405.           adj->pinger = newproc("RSPF ping",1024,rspfping,
  406.                     (int)adj->state,adj,NULL);
  407.           adj->state = RSPF_SUSPECT; /* The adjacency is now Suspect */
  408.      } else {        /* Try later */
  409.           adj->timer.start = dur_timer(&Susptimer);
  410.           start_timer(&adj->timer);
  411.      }
  412.      break;
  413.     case RSPF_BAD:
  414.          rr_delete(adj->addr);
  415.      adj->cost = 255;
  416.      if(adj->okcnt != 0)
  417.           goodbadnews(adj);        /* This is bad news... */
  418.      if(prev != NULLADJ)
  419.           prev->next = adj->next;
  420.      else
  421.           Adjs = adj->next;
  422.      stop_timer(&adj->timer);    /* just in case */
  423.      free((char *)adj);        /* delete the adjacency */
  424.      makeroutes();            /* update the routing table */
  425.      break;
  426.     }
  427. }
  428.  
  429. /* Send a ping to a suspect or tentative adjacency */
  430. static void
  431. rspfping(oldstate, p, v)
  432. int oldstate;
  433. void *p, *v;
  434. {
  435.     int s, fromlen, pcnt;
  436.     struct icmp icmp;
  437.     struct rspfadj *adj;
  438.     struct sockaddr_in from;
  439.     struct mbuf *bp;
  440.     pause(((ptol(p) & 7)+1)*1000/MSPTICK);    /* Pause for 1 to 8 seconds */
  441.     adj = (struct rspfadj *) p;
  442.     adj->timer.func = rspfevent;        /* Fill in timer values */
  443.     adj->timer.arg = (void *) &adj->timer;
  444.     adj->timer.start = dur_timer(&Susptimer);
  445.     if((s = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP)) == -1){
  446.     --Nrspfproc;
  447.     adj->state = oldstate;
  448.     start_timer(&adj->timer);
  449.     return;
  450.     }
  451.     fromlen = sizeof(from);
  452.     for(pcnt=0; pcnt < Rspfpingmax; ++pcnt) {
  453.     pingem(s,adj->addr,0,(int16)s,0);
  454.     /* Now collect the reply */
  455.     alarm(30 * 1000/MSPTICK);/* Let each ping timeout after 30 seconds */
  456.     for(;;) {
  457.          if(recv_mbuf(s,&bp,0,(char *)&from,&fromlen) == -1){
  458.           if(errno == EALARM) /* We timed out */
  459.                break;
  460.           alarm(0);
  461.           adj->state = oldstate;
  462.           close_s(s);
  463.           --Nrspfproc;
  464.           start_timer(&adj->timer);
  465.           return;
  466.          }
  467.          ntohicmp(&icmp,&bp);
  468.          free_p(bp);
  469.          if(icmp.type != ICMP_ECHO_REPLY || from.sin_addr.s_addr !=
  470.         adj->addr || icmp.args.echo.id != s)
  471.           /* Ignore other people's responses */
  472.           continue;
  473.          alarm(0);
  474.          if(++adj->okcnt == 1)
  475.           goodbadnews(adj);        /* Good news */
  476.          close_s(s);
  477.          --Nrspfproc;
  478.          start_timer(&adj->timer);
  479.          adj->state = RSPF_OK;        /* Finally change state */
  480.          return;
  481.     }
  482.     }
  483.     /* we give up, mark the adjacency as bad */
  484.     adj->state = RSPF_BAD;
  485.     close_s(s);
  486.     --Nrspfproc;
  487.     start_timer(&adj->timer);
  488. }
  489.  
  490. /* ARP upcalls come in two flavors: When an ARP Reply has been received, we
  491.  * know for certain that bidirectional communication is possible with the
  492.  * particular station. But when an ARP Request is received, or if an ARP
  493.  * entry is added manually, it has yet to be determined if the link is
  494.  * bidirectional so iface is NULLIF in those cases.
  495.  */
  496. void
  497. rspfarpupcall(addr,hardware,iface)
  498. int32 addr;            /* Address being added to ARP table */
  499. int16 hardware;            /* Hardware type */
  500. struct iface *iface;        /* Interface used, if known */
  501. {
  502.     struct rspfadj *adj;
  503.     struct mbuf *bp;
  504.     struct rspfiface *riface;
  505.     if((riface = rtif_lookup(addr)) == NULLRIFACE)
  506.      return;        /* Not a route to an RSPF interface */
  507.     /* Proceed only if the ARP entry is for a hardware type that is relevant
  508.      * for the interface onto which IP datagrams are routed.
  509.      */
  510.     switch(hardware) {
  511.     case ARP_NETROM:
  512.      if(riface->iface->type != TYPE_NETROM)
  513.           return;
  514.      break;
  515.     case ARP_AX25:
  516.      if(riface->iface->type != TYPE_AX25)
  517.           return;
  518.      break;
  519.     case NHWTYPES:
  520.      break;        /* "Any interface type is ok" type entry */
  521.     default:
  522.      return;
  523.     }
  524.     if((adj = (struct rspfadj *)calloc(1,sizeof(struct rspfadj)))==NULLADJ)
  525.     return;
  526.     adj->addr = addr;
  527.     if(iface == NULLIF)      /* A manual ARP entry or Request, may be no-good */
  528.     adj->state = RSPF_TENTATIVE;
  529.     else {
  530.     adj->state = RSPF_OK;
  531.     adj->okcnt++;
  532.     adj->timer.func = rspfevent;        /* Fill in timer values */
  533.     adj->timer.arg = (void *) &adj->timer;
  534.     adj->timer.start = dur_timer(&Susptimer);
  535.     start_timer(&adj->timer);    
  536.     }
  537.     if((bp = alloc_mbuf(1+sizeof(int32)+sizeof(iface))) == NULLBUF) {
  538.      stop_timer(&adj->timer);
  539.      free((char *)adj);
  540.      return;
  541.     }
  542.     *bp->data = RSPFE_ARP;
  543.     memcpy(bp->data + 1, (char *) &adj, sizeof(adj));
  544.     memcpy(bp->data + 1 + sizeof(adj), (char *) &iface, sizeof(iface));
  545.     bp->cnt = bp->size;
  546.     enqueue(&Rspfinq,bp);
  547. }
  548.  
  549. /* Called by "route add" command. */
  550. void
  551. rspfrouteupcall(addr,bits,gateway)
  552. int32 addr;            /* Address added to routing table */
  553. unsigned bits;            /* Significant bits in address */
  554. int32 gateway;            /* Address of gateway station */
  555. {
  556.     /* We are only interested in 32 bit specific routes that use a
  557.      * gateway. Direct routes will be discovered anyway.
  558.      */
  559.     if(bits != 32 || gateway == 0 || gateway == addr)
  560.      return;
  561.     if(rtif_lookup(addr) == NULLRIFACE) /* not routed onto RSPF interface */
  562.      return;
  563.     rspfarpupcall(addr,NHWTYPES,NULLIF); /* proceed as an "arp add" upcall */
  564. }
  565.  
  566. /* When the suspect timer expires, we scan through the routing table for all
  567.  * manual 32 bit specific routes through a gateway that are not an adjacency,
  568.  * and calls rspfrouteupcall(). A similar thing is done for all manual ARP
  569.  * entries. This will make sure that a station that was not reachable when
  570.  * the "route add" or "arp add" command was executed will eventually be
  571.  * discovered if it joins the network.
  572.  */
  573. void
  574. rspfsuspect(t)
  575. void *t;
  576. {
  577.      struct rspfadj *adj;
  578.      struct route *rp;
  579.      struct arp_tab *ap;
  580.      int i;
  581.      start_timer(&Susptimer);            /* restart the timer */
  582.      for(i = 0; i < NROUTE; i++)        /* Check the routing table */
  583.       for(rp = Routes[31][i]; rp != NULLROUTE; rp = rp->next) {
  584.            if((rp->flags & RTPRIVATE) || dur_timer(&rp->timer) != 0)
  585.             continue;    /* not this route */
  586.            for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  587.             if(adj->addr == rp->target)
  588.              break;
  589.            if(adj == NULLADJ) /* it is not already an adjacency */
  590.             rspfrouteupcall(rp->target,32,rp->gateway);
  591.       }
  592.      for(i=0; i < ARPSIZE; ++i)    /* scan the ARP table */
  593.       for(ap = Arp_tab[i]; ap != NULLARP; ap = ap->next) {
  594.            if(dur_timer(&ap->timer))    /* not a manual entry */
  595.             continue;
  596.            for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  597.             if(adj->addr == ap->ip_addr)
  598.              break;
  599.            if(adj == NULLADJ)    /* not already an adjacency */
  600.             rspfarpupcall(ap->ip_addr,ap->hardware,NULLIF);
  601.       }
  602. }
  603.  
  604. /* This non-layered function peeks at the routing table to figure out to which
  605.  * interface datagrams for addr will be routed. Then it returns the matching
  606.  * rspfiface structure.
  607.  */
  608. static
  609. struct rspfiface *
  610. rtif_lookup(addr)
  611. int32 addr;
  612. {
  613.     struct rspfiface *riface;
  614.     struct route *rp;
  615.     if((rp = rt_lookup(addr)) == NULLROUTE)
  616.     return NULLRIFACE;
  617.     riface = Rspfifaces;
  618.     while(riface != NULLRIFACE){
  619.     if(riface->iface == rp->iface)
  620.         return riface;
  621.     riface = riface->next;
  622.     }
  623.     return NULLRIFACE;
  624. }
  625.  
  626. /* Send good or bad news partial routing updates. A cost of 255 should be
  627.  * interpreted as bad news by the remote station.
  628.  */
  629. static void
  630. goodbadnews(adj)
  631. struct rspfadj *adj;
  632. {
  633.     struct rspfnodeh nodeh;
  634.     struct rspflinkh linkh;
  635.     struct rspfiface *riface;
  636.     struct rspfrouter *rr;
  637.     struct mbuf *bp, *tbp, *wbp;
  638.     int nodecnt = 1;
  639.     if((riface = rtif_lookup(adj->addr)) == NULLRIFACE)
  640.     return;
  641.     bp = alloc_mbuf(5);
  642.     bp->cnt = bp->size;
  643.     *bp->data = 32;     /* the number of significant bits in the address */
  644.     put32(bp->data+1,adj->addr);
  645.     linkh.horizon = riface->horizon;
  646.     linkh.erp = riface->erp;
  647.     linkh.cost = adj->cost;
  648.     linkh.adjn = 1;
  649.     tbp = htonrspflink(&linkh,bp);
  650.     nodeh.seq = Rspfseq;
  651.     nodeh.subseq = ++Rspfsubseq;
  652.     nodeh.links = 1;
  653.     for(riface = Rspfifaces; riface != NULLRIFACE; riface = riface->next) {
  654.       if(dup_p(&wbp,tbp,0,len_p(tbp)) != len_p(tbp)) {
  655.            free_p(wbp);
  656.            continue;
  657.        }
  658.        nodeh.addr = riface->iface->addr;
  659.        bp = htonrspfnode(&nodeh,wbp);
  660.        sendtoall(bp,1,riface);
  661.     }
  662.     free_p(tbp);
  663.     /* If this is a new adjacency, then send it a routing update immediately */
  664.     if(adj->cost == 255 || adj->okcnt != 1)
  665.     return;
  666.     /* When two RSPF stations learn about each others existence, one of
  667.      * them will usually have received an RRH from the other. The one that
  668.      * received the RRH will send the peer a routing update immediately.
  669.      * So in the code below, if no RRH has been received (seq is 0) and no
  670.      * routing update has yet been received, we should expect to receive a
  671.      * routing update shortly if the adjacency is running RSPF.
  672.      * This could fail though if two RSPF stations learn about each other
  673.      * solely through ARP upcalls and no RRH's or Updates were exchanged
  674.      * prior to or during the establishment of the adjacency.
  675.      */
  676.     if(adj->seq == 0 && rr_lookup(adj->addr) == NULLRROUTER) {
  677.     if(adj->state != RSPF_SUSPECT)    /* running in RSPF process, give up */
  678.         return;
  679.     alarm(15 * 1000/MSPTICK);    /* wait for an Update */
  680.     if(pwait(adj) == EALARM)
  681.          return;    /* still no sign of RSPF activity from the adjacency */
  682.     alarm(0);
  683.     }
  684.     ++adj->okcnt;    /* we don't want to get here again */
  685.     if((bp = makeownupdate(adj->addr,1)) == NULLBUF)
  686.     return;
  687.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  688.     if((tbp = makeadjupdate(get32(rr->data->data),rr)) != NULLBUF){
  689.          append(&bp,tbp);
  690.          nodecnt++;
  691.     }
  692.     sendupdate(bp,nodecnt,adj->addr);
  693. }
  694.  
  695. /* This function is called whenever it is time to send a new RSPF update */
  696. static void
  697. sendrspf()
  698. {
  699.     struct rspfrouter *rr;
  700.     struct mbuf *bp, *wbp, *tmp = NULLBUF;
  701.     struct rspfiface *riface;
  702.     struct rspfadj *adj;
  703.     int nodecnt, incr = 1;
  704.  
  705.     for(nodecnt = 1, rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  706.      if(!rr->sent)        /* don't send stale data */
  707.           if((bp = makeadjupdate(get32(rr->data->data),rr)) != NULLBUF){
  708.            append(&tmp,bp);
  709.            nodecnt++;
  710.           }
  711.     for(riface = Rspfifaces; riface != NULLRIFACE; riface = riface->next) {
  712.      /* Find an address that is routed onto this interface */
  713.      for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  714.           if(rtif_lookup(adj->addr) == riface)
  715.            break;
  716.      if(adj == NULLADJ)    /* no adjacency on that interface? */
  717.           continue;
  718.      if(dup_p(&wbp,tmp,0,len_p(tmp)) != len_p(tmp)) {
  719.           free_p(wbp);
  720.           continue;
  721.      }
  722.      if((bp = makeownupdate(adj->addr,incr)) != NULLBUF) {
  723.           incr = 0;    /* sequence number is incremented only once */
  724.           append(&bp,wbp);
  725.      }
  726.      else
  727.           break;
  728.      sendtoall(bp,nodecnt,riface);
  729.     }
  730.     free_p(tmp);
  731.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  732.      if(!rr->sent)        /* Mark router data as propagated */
  733.           ++rr->sent;
  734. }
  735.  
  736. /* This function is used to answer "poll" messages */
  737. static void
  738. sendonerspf(addr,dest)
  739. int32 addr;    /* address of station whose routing update we will make */
  740. int32 dest;    /* address of station to send the update to */
  741. {
  742.     struct mbuf *bp;
  743.     if(ismyaddr(addr)){
  744.     if((bp = makeownupdate(dest,0)) == NULLBUF)
  745.         return;
  746.     sendupdate(bp,1,dest);
  747.     return;
  748.     }
  749.     if((bp = makeadjupdate(addr,NULLRROUTER)) == NULLBUF)
  750.     return;
  751.     sendupdate(bp,1,dest);
  752. }
  753.     
  754. /* send an update to all adjacencies on an RSPF interface */
  755. static void
  756. sendtoall(bp,nodecnt,riface)
  757. struct mbuf *bp;
  758. int nodecnt;            /* number of reporting nodes in update */
  759. struct rspfiface *riface;    /* interface to sent to */
  760. {
  761.      struct rspfadj *adj;
  762.      struct mbuf *wbp;
  763.      int broad;
  764.      for(broad = 0, adj = Adjs; adj != NULLADJ; adj = adj->next)
  765.       /* If adj->seq is 0 we have never received an RRH from the
  766.        * adjacency, and if there is no stored routing update, then
  767.        * it is not known if the adjacency understands RSPF.
  768.        */
  769.       if((adj->seq != 0 || rr_lookup(adj->addr) != NULLRROUTER) &&
  770.          riface == rtif_lookup(adj->addr)) {
  771.            if((adj->tos & RELIABILITY) && !(adj->tos & DELAY)) {
  772.             if(dup_p(&wbp,bp,0,len_p(bp)) != len_p(bp)) {
  773.              free_p(wbp);
  774.              continue;
  775.             }
  776.             sendupdate(wbp,nodecnt,adj->addr); /* private copy */
  777.            }
  778.            else
  779.             ++broad;    /* send to broadcast IP address instead */
  780.       }
  781.      if(broad != 0)
  782.       if(dup_p(&wbp,bp,0,len_p(bp)) != len_p(bp))
  783.            free_p(wbp);
  784.       else
  785.            sendupdate(wbp,nodecnt,riface->iface->broadcast);
  786.      free_p(bp);
  787. }     
  788.  
  789. /* This function sends a routing update to the adjacencies that we have
  790.  * recevied RRH's from.
  791.  */
  792. static int
  793. sendupdate(bp,nodecnt,addr)
  794. struct mbuf *bp;    /* Full packet, except for the packet header */
  795. int nodecnt;        /* Number of reporting nodes in the packet */
  796. int32 addr;        /* Station to send update to */
  797. {
  798.     struct rspfadj *adj;
  799.     struct mbuf *tmp;
  800.     int tos = 0;
  801.  
  802.     /* See if we are sending to a known adjacency, in use its TOS in
  803.      * that case.
  804.      */
  805.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  806.      if(adj->addr == addr) {
  807.           tos = adj->tos;
  808.           break;
  809.      }
  810.     if((tmp = fragmenter(bp,nodecnt,ip_mtu(addr) - IPLEN)) == NULLBUF)
  811.      return -1;
  812.     while((bp = dequeue(&tmp)) != NULLBUF){
  813.      rspfcsum(&bp,addr);    /* Calculate the checksum */
  814.      ++Rspf_stat.updateout;
  815.      ip_send(INADDR_ANY,addr,RSPF_PTCL,INET_CTL | tos,2,bp,0,0,0);
  816.     }
  817.     return 0;
  818. }
  819.  
  820. /* Fragment a large mbuf if necessary into packets of maximum mtu bytes.
  821.  * Each packet is prepended a RSPF routing update header, without the checksum.
  822.  */
  823. static struct mbuf *
  824. fragmenter(bp,nodes,mtu)
  825. struct mbuf *bp; /* packet to send, without packet header */
  826. int nodes;    /* The number of reporting nodes in the routing update */
  827. int16 mtu;    /* The maximum transmission unit */
  828. {
  829.     struct rspfnodeh nodeh;
  830.     struct rspflinkh linkh;
  831.     struct rspfpacketh pkth;
  832.     struct mbuf *tbp, *tmp, *bpq = NULLBUF;
  833.     int n, nodecnt, linkcnt, adjcnt, nodemax, sync;
  834.     char *cp, fragn = 1;
  835.  
  836.     if(mtu < RSPFPKTLEN + RSPFNODELEN || bp == NULLBUF) {
  837.      free_p(bp);
  838.      return NULLBUF;
  839.     }
  840.     ++Envelopeid;
  841.     nodemax = nodes;        /* total number of nodes in envelope */
  842.     nodecnt = nodes;        /* nodes left to packetize */
  843.     linkcnt = adjcnt = 0;
  844.     while(len_p(bp) != 0){
  845.     n = min(mtu,len_p(bp)+RSPFPKTLEN);    /* length of this packet */
  846.     n -= RSPFPKTLEN;
  847.     tbp = NULLBUF;
  848.     sync = 0;
  849.     if(adjcnt){
  850.         tbp = alloc_mbuf(min(adjcnt*5,n/5*5));
  851.         tbp->cnt = tbp->size;
  852.         cp = tbp->data;
  853.     }
  854.     while(n > 0){
  855.         while(adjcnt){
  856.         if((n -= 5) < 0)
  857.             break;
  858.         pullup(&bp,cp,5);
  859.         cp += 5;
  860.         adjcnt--;
  861.         }
  862.         if(linkcnt && n > 0){
  863.         if((n -= RSPFLINKLEN) < 0)
  864.             break;
  865.         ntohrspflink(&linkh,&bp);
  866.         adjcnt = linkh.adjn;
  867.         tmp = htonrspflink(&linkh,NULLBUF);
  868.         append(&tbp,tmp);
  869.         tmp = alloc_mbuf(min(5*adjcnt,n/5*5));
  870.         tmp->cnt = tmp->size;
  871.         cp = tmp->data;
  872.         append(&tbp,tmp);
  873.         linkcnt--;
  874.         continue;
  875.         }
  876.         if(nodecnt && linkcnt == 0 && n > 0){
  877.         if((n -= RSPFNODELEN) < 0)
  878.             break;
  879.         if(nodecnt == nodes)        /* Set the sync octet */
  880.             sync = len_p(tbp) + 4;
  881.         ntohrspfnode(&nodeh,&bp);
  882.         linkcnt = nodeh.links;
  883.         tmp = htonrspfnode(&nodeh,NULLBUF);
  884.         append(&tbp,tmp);
  885.         nodecnt--;
  886.         }
  887.     }
  888.     pkth.version = RSPF_VERSION;    /* The version number */
  889.     pkth.type = RSPF_FULLPKT;    /* The packet type */
  890.     pkth.fragn = fragn++;        /* The fragment number */
  891.     pkth.fragtot = 0;        /* The total number of fragments */
  892.     pkth.csum = 0;            /* The checksum */
  893.     pkth.sync = sync < 256 ? sync : 0;    /* The sync octet */
  894.     pkth.nodes = nodemax;        /* The number of nodes in envelope */
  895.     pkth.envid = Envelopeid;    /* The envelope-ID */
  896.     tmp = htonrspf(&pkth,tbp);    /* add packet header */
  897.     enqueue(&bpq,tmp);        /* add packet to chain */
  898.     nodes = nodecnt;        /* number of nodes left */
  899.     }
  900.     free_p(bp);
  901.     for(tbp = bpq; tbp != NULLBUF; tbp = tbp->anext)
  902.     *(tbp->data + 3) = len_q(bpq);    /* Set the fragment total counter */
  903.     return bpq;
  904. }
  905.  
  906. /* Calculate the checksum on an RSPF routing update packet */
  907. static void
  908. rspfcsum(bpp,dest)
  909. struct mbuf **bpp;
  910. int32 dest;
  911. {
  912.     struct pseudo_header ph;
  913.     int16 checksum;
  914.     ph.length = len_p(*bpp);
  915.     ph.source = locaddr(dest);
  916.     ph.dest = dest;
  917.     ph.protocol = RSPF_PTCL;
  918.     if((checksum = cksum(&ph,*bpp,ph.length)) == 0)
  919.     checksum = 0xffffffff;
  920.     /* This assumes that the checksum really is at this position */
  921.     put16((*bpp)->data+4,checksum);
  922. }
  923.  
  924. /* This function creates our own routing update and returns it in an mbuf */
  925. struct mbuf *
  926. makeownupdate(dest,new)
  927. int32 dest;        /* Address of a station that will get this update */
  928. int new;        /* True if the sequence number should be incremented */
  929. {
  930.     struct rspfadj *adj;
  931.     struct rspflinkh linkh;
  932.     struct rspfnodeh nodeh;
  933.     struct rspfiface *riface, rifdefault;
  934.     struct mbuf *bp = NULLBUF, *tbp, *tmp;
  935.     struct route *rp;
  936.     int i, adjcnt, lnkcnt, bits;
  937.     int32 prev, low, cur;
  938.     /* Set default values to apply to non-RSPF interfaces */
  939.     rifdefault.horizon = 32;
  940.     rifdefault.erp = 0;
  941.     rifdefault.quality = 0;
  942.     /* Our adjacencies has to be sorted into groups of the same horizon,
  943.      * ERP factor and cost. This is done by combining these numbers into a
  944.      * single one, modulo 256 and take the lowest number first.
  945.      */
  946.     low = 0;
  947.     lnkcnt = 0;
  948.     for(;;){
  949.     prev = low;
  950.     low = 255*65536+255*256+255;
  951.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  952.          /* Include all adjacecies that have been or are in OK state
  953.           * in the update. Bad adjacencies are also included to give
  954.           * them a chance to recover. Hopelessly Bad adjacencies are
  955.           * eventually deleted elsewhere.
  956.           */
  957.         if(adj->okcnt != 0 && (riface = rtif_lookup(adj->addr)) !=
  958.            NULLRIFACE) {
  959.         cur = riface->horizon*65536+riface->erp*256+adj->cost;
  960.         if(cur > prev && cur < low)
  961.             low = cur;
  962.         }
  963.     /* Add any manual public, 1-31 bit specific routes.
  964.      * Use the route metric only if it is greater than the default
  965.      * quality to lessen a possible "wormhole" effect.
  966.      */
  967.     for(bits = 1; bits <= 31; bits++)
  968.         for(i = 0; i < NROUTE; i++)
  969.         for(rp = Routes[bits-1][i]; rp != NULLROUTE; rp = rp->next)
  970.              if(!(rp->flags & RTPRIVATE) && !dur_timer(&rp->timer)) {
  971.              if((riface = rtif_lookup(rp->target)) == NULLRIFACE)
  972.                     riface = &rifdefault;
  973.              cur = riface->horizon*65536+riface->erp*256+
  974.                 (rp->metric > riface->quality ? rp->metric :
  975.                 riface->quality);
  976.             if(cur > prev && cur < low)
  977.                 low = cur;
  978.              }
  979.     /* Add any 32 bit routes on interfaces not using RSPF */
  980.     for(i = 0; i < NROUTE; i++)
  981.          for(rp = Routes[31][i]; rp != NULLROUTE; rp = rp->next)
  982.           if(!(rp->flags & RTPRIVATE) && rtif_lookup(rp->target)
  983.              == NULLRIFACE) {
  984.                cur = rifdefault.horizon*65536+rifdefault.erp*256+
  985.                 (rp->metric > rifdefault.quality ? rp->metric :
  986.                  rifdefault.quality);
  987.                if(cur > prev && cur < low)
  988.                 low = cur;
  989.           }
  990.     /* Add the default route */
  991.     if((rp = rt_blookup(0,0)) != NULLROUTE && !(rp->flags & RTPRIVATE) &&
  992.        !dur_timer(&rp->timer)) {
  993.          if((riface = rtif_lookup(0)) == NULLRIFACE)
  994.           riface = &rifdefault;
  995.          cur = riface->horizon*65536+riface->erp*256+
  996.         (rp->metric > riface->quality ? rp->metric : riface->quality);
  997.          if(cur > prev && cur < low)
  998.           low = cur;
  999.     }
  1000.     if(low == 255*65536+255*256+255) /* All done */
  1001.         break;
  1002.     /* now start adding the routes */
  1003.     adjcnt = 0;
  1004.     tbp = NULLBUF;
  1005.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  1006.         if(adj->okcnt != 0 && (riface = rtif_lookup(adj->addr)) !=
  1007.            NULLRIFACE)
  1008.         if(riface->horizon*65536+riface->erp*256+adj->cost == low){
  1009.             pushadj(&tbp,adj->addr,32);
  1010.             adjcnt++;
  1011.         }
  1012.     for(bits = 1; bits <= 31; bits++)
  1013.         for(i = 0; i < NROUTE; i++)
  1014.         for(rp = Routes[bits-1][i]; rp != NULLROUTE; rp = rp->next)
  1015.              if(!(rp->flags & RTPRIVATE) && !dur_timer(&rp->timer)){
  1016.             if((riface = rtif_lookup(rp->target)) == NULLRIFACE)
  1017.                    riface = &rifdefault;
  1018.             /* Manually entered local routes only */
  1019.             if(riface->horizon*65536+riface->erp*256+
  1020.                (rp->metric > riface->quality ? rp->metric :
  1021.                riface->quality) == low){
  1022.                 pushadj(&tbp,rp->target,bits);
  1023.                 adjcnt++;
  1024.             }
  1025.              }
  1026.     for(i = 0; i < NROUTE; i++)    /* 32 bit specific routes */
  1027.          for(rp = Routes[31][i]; rp != NULLROUTE; rp = rp->next)
  1028.           if(!(rp->flags & RTPRIVATE) && rtif_lookup(rp->target)
  1029.              == NULLRIFACE)
  1030.                if(rifdefault.horizon*65536+rifdefault.erp*256+
  1031.               (rp->metric > rifdefault.quality ? rp->metric :
  1032.                rifdefault.quality) == low){
  1033.                 pushadj(&tbp,rp->target,bits);
  1034.                 adjcnt++;
  1035.                }
  1036.     /* Add the default route */
  1037.     if((rp = rt_blookup(0,0)) != NULLROUTE && !(rp->flags & RTPRIVATE) &&
  1038.        !dur_timer(&rp->timer)) {
  1039.          if((riface = rtif_lookup(0)) == NULLRIFACE)
  1040.           riface = &rifdefault;
  1041.          if(riface->horizon*65536+riface->erp*256+ (rp->metric > 
  1042.         riface->quality ? rp->metric : riface->quality) == low){
  1043.           pushadj(&tbp,0,0);
  1044.           adjcnt++;
  1045.          }
  1046.     }
  1047.     if(adjcnt != 0){
  1048.         /* Prepend the link header */
  1049.         linkh.horizon = ((low >> 16) & 255);/* Horizon value */
  1050.         linkh.erp = ((low >> 8) & 255);    /* ERP factor */
  1051.         linkh.cost = (low & 255);        /* Link cost */
  1052.         linkh.adjn = adjcnt;        /* Number of adjacencies */
  1053.         tmp = htonrspflink(&linkh,tbp);
  1054.         append(&bp,tmp);
  1055.         lnkcnt++;
  1056.     }
  1057.     }
  1058.     /* Prepend the node header */
  1059.     if(lnkcnt != 0){
  1060.     /* Set our address to the IP address used on the destinations
  1061.      * interface.
  1062.      */
  1063.     if(dest == INADDR_ANY || (riface = rtif_lookup(dest)) == NULLRIFACE)
  1064.          nodeh.addr = Ip_addr;
  1065.     else
  1066.          nodeh.addr = riface->iface->addr;
  1067.     if(new){    /* increase sequence number, clear subseq. number */
  1068.         if(Rspfseq == 32768 - 2 || Rspfseq == -32768 + 1)
  1069.          Rspfseq = 0;            /* wrap around */
  1070.         else
  1071.          ++Rspfseq;
  1072.         Rspfsubseq = 0;
  1073.     }
  1074.     nodeh.seq = Rspfseq;
  1075.     nodeh.subseq = 0;
  1076.     nodeh.links = lnkcnt;
  1077.     return htonrspfnode(&nodeh,bp);
  1078.     }
  1079.     else {
  1080.     free_p(bp);
  1081.     return NULLBUF;
  1082.     }
  1083. }
  1084. /* Prepends an adjacency to bpp. Allocates bpp in large chunk for efficency */
  1085. static void
  1086. pushadj(bpp,addr,bits)
  1087. struct mbuf **bpp;
  1088. int32 addr;
  1089. int bits;
  1090. {
  1091.      if(bpp == NULLBUFP)
  1092.       return;
  1093.      if(*bpp == NULLBUF) {
  1094.       *bpp = alloc_mbuf(60);        /* good for 12 adjacencies */
  1095.       (*bpp)->data += 55;
  1096.       (*bpp)->cnt = 5;
  1097.      }
  1098.      else
  1099.       *bpp = pushdown(*bpp,5);
  1100.      *(*bpp)->data = bits;
  1101.      put32((*bpp)->data+1,addr);
  1102. }
  1103.  
  1104. /* This function returns the latest routing update in network fromat from
  1105.  * the adjacency denoted by addr.
  1106.  */
  1107. static struct mbuf *
  1108. makeadjupdate(addr,rr)
  1109. int32 addr;
  1110. struct rspfrouter *rr;    /* pointer to stored routing entry, if known */
  1111. {
  1112.     struct mbuf *tmp, *tmp2, *tmp3, *bp = NULLBUF;
  1113.     struct rspfnodeh nodeh;
  1114.     struct rspflinkh linkh;
  1115.     int linkcnt = 0;
  1116.     if(rr == NULLRROUTER && (rr = rr_lookup(addr)) == NULLRROUTER)
  1117.     return NULLBUF;
  1118.     if(dup_p(&tmp,rr->data,0,len_p(rr->data)) != len_p(rr->data)) {
  1119.      free_p(tmp);
  1120.      return NULLBUF;
  1121.     }
  1122.     ntohrspfnode(&nodeh,&tmp);             /* Get the node header */
  1123.     while(nodeh.links--){
  1124.     ntohrspflink(&linkh,&tmp);
  1125.     /* Decrement and check the horizon value */
  1126.     if(--linkh.horizon > 0){
  1127.         /* Duplicate the adjacencies */
  1128.         if(dup_p(&tmp2,tmp,0,5*linkh.adjn) != 5*linkh.adjn){
  1129.         free_p(tmp);
  1130.         free_p(tmp2);
  1131.         free_p(bp);
  1132.         return NULLBUF;
  1133.         }
  1134.         /* Prepend the link header */
  1135.         tmp3 = htonrspflink(&linkh,tmp2);
  1136.         append(&bp,tmp3);
  1137.         linkcnt++;
  1138.     }
  1139.     pullup(&tmp,NULLCHAR,5*linkh.adjn); /* Skip the adjacencies */
  1140.     }
  1141.     free_p(tmp);
  1142.     if(linkcnt == 0){         /* No links at all? */
  1143.     free_p(bp);
  1144.     return NULLBUF;
  1145.     }
  1146.     nodeh.subseq = rr->subseq;
  1147.     nodeh.links = linkcnt;
  1148.     /* Prepend the node header */
  1149.     return htonrspfnode(&nodeh,bp);
  1150. }
  1151.  
  1152. /* Returns 1 if sequence number a is newer than b. Sequence numbers start
  1153.  * at -32768 + 1 and then continues with 0 and the positive integer numbers
  1154.  * until reaching 32768 - 2 after which they continue with 0.
  1155.  * Algorithm taken from RFC-1131 but fixed bug when a or b is 0.
  1156.  */
  1157. static int
  1158. isnewer(a,b)
  1159. short a,b;
  1160. {
  1161.      if((b < 0 && a > b) ||
  1162.     (a >= 0 && b >= 0 &&    /* bug corrected here */
  1163.      (((32768 - 1)/2 > (a-b) && (a-b) > 0) ||
  1164.       (a-b) < -(32768 - 1)/2)))
  1165.       return 1;
  1166.      return 0;
  1167. }
  1168.      
  1169. /* Reassemble possibly fragmented packets into a single large one */
  1170. static struct mbuf *
  1171. rspfreasm(bp,rph,addr)
  1172. struct mbuf *bp;      /* Routing update packet without packet header */
  1173. struct rspfpacketh *rph;  /* Packet header */
  1174. int32 addr;          /* Address of station we got the packet from */
  1175. {
  1176.     union rspf rspf;
  1177.     struct rspfreasm *re, *rtmp;
  1178.     struct mbuf *tbp, *bp1, *bp2;
  1179.     for(re = Rspfreasmq; re != NULLRREASM; re = re->next)
  1180.     if(re->addr == addr){    /* found another fragment */
  1181.         if(dup_p(&tbp,re->data,0,RSPFPKTLEN) != RSPFPKTLEN) {
  1182.          free_p(tbp);
  1183.          free_p(bp);
  1184.          return NULLBUF;
  1185.         }
  1186.         ntohrspf(&rspf,&tbp);    /* get its packet header */
  1187.         if(rph->envid != rspf.pkthdr.envid) {
  1188.          del_reasm(re);        /* an obsolete entry, delete it */
  1189.          break;
  1190.         }
  1191.         /* Now try to find a place where this fragment fits in the chain */
  1192.         bp1 = re->data;
  1193.         bp2 = NULLBUF;
  1194.         while(rph->fragn > rspf.pkthdr.fragn && bp1->anext != NULLBUF){
  1195.         bp2 = bp1;
  1196.         bp1 = bp1->anext;
  1197.         if(dup_p(&tbp,bp1,0,RSPFPKTLEN) != RSPFPKTLEN) {
  1198.              free_p(bp);
  1199.              free_p(tbp);
  1200.              return NULLBUF;
  1201.         }
  1202.         ntohrspf(&rspf,&tbp);
  1203.         }
  1204.         if(rspf.pkthdr.fragn == rph->fragn) {
  1205.          free_p(bp);
  1206.          return NULLBUF;    /* We already had this one */
  1207.         }
  1208.         bp = htonrspf(rph,bp);    /* Put the packet header back on */
  1209.         /* Insert the fragment at the right place in the chain */
  1210.         if(rph->fragn > rspf.pkthdr.fragn){
  1211.         bp1->anext = bp;
  1212.         bp->anext = NULLBUF;
  1213.         }
  1214.         else {
  1215.         if(bp2 != NULLBUF)
  1216.             bp2->anext = bp;
  1217.         else
  1218.             re->data = bp;
  1219.         bp->anext = bp1;
  1220.         }
  1221.         if(len_q(re->data) == rspf.pkthdr.fragtot){ 
  1222.         bp1 = NULLBUF;            /* we have all the fragments */
  1223.         while((bp2 = dequeue(&re->data)) != NULLBUF){
  1224.             pullup(&bp2,NULLCHAR,RSPFPKTLEN); /* strip the headers */
  1225.             append(&bp1,bp2);        /* and form a single packet */
  1226.         }
  1227.         del_reasm(re);
  1228.         stop_timer(&Rspfreasmt);
  1229.         reasmtimeout(NULL);    /* restarts timer if necessary */
  1230.         return bp1;        /* return the completed packet */
  1231.         }
  1232.         re->time = Clock;        /* Update the timestamp */
  1233.         goto timstart;        /* We have to wait for fragments */
  1234.     }
  1235.     /* At this point we know that there is no matching entry in the
  1236.        reassembly queue (any more) */
  1237.     if(rph->fragtot == 1)    /* Simple case, an un-fragmented packet */
  1238.     return bp;
  1239.     tbp = htonrspf(rph,bp);    /* Put the packet header back on */
  1240.     rtmp = (struct rspfreasm *) callocw(1,sizeof(struct rspfreasm));
  1241.     /* The values are filled in */
  1242.     rtmp->addr = addr;
  1243.     rtmp->time = Clock;
  1244.     rtmp->data = tbp;
  1245.     rtmp->next = Rspfreasmq;
  1246.     Rspfreasmq = rtmp;
  1247.  timstart:                    /* Start the timer if needed */
  1248.     if(!run_timer(&Rspfreasmt)){
  1249.     Rspfreasmt.func = reasmtimeout;
  1250.     Rspfreasmt.arg = NULL;
  1251.     set_timer(&Rspfreasmt,RSPF_RTIME*1000);    /* The reassembly timeout */
  1252.     start_timer(&Rspfreasmt);
  1253.     }
  1254.     return NULLBUF;
  1255. }
  1256.  
  1257. /* Delete a reassembly descriptor from the queue */
  1258. static void
  1259. del_reasm(re)
  1260. struct rspfreasm *re;
  1261. {
  1262.     struct rspfreasm *r, *prev = NULLRREASM;
  1263.     for(r = Rspfreasmq; r != NULLRREASM; prev = r, r = r->next)
  1264.     if(r == re){
  1265.         free_q(&re->data);
  1266.         if(prev != NULLRREASM)
  1267.         prev->next = re->next;
  1268.         else
  1269.         Rspfreasmq = re->next;
  1270.         free((char *)re);
  1271.         break;
  1272.     }
  1273. }
  1274.  
  1275. /* When the reassembly timer times out, this function tries to make use of
  1276.  * any fragments in the reassembly queue.
  1277.  */
  1278. static void
  1279. reasmtimeout(t)
  1280. void *t;
  1281. {
  1282.     union rspf rspf;
  1283.     struct rspfreasm *re;
  1284.     struct mbuf *bp, *tbp;
  1285.     int last = 0;
  1286.     int32 low;
  1287.     re = Rspfreasmq;
  1288.     while(re != NULLRREASM)
  1289.     if((Clock - re->time) >= RSPF_RTIME*1000/MSPTICK){
  1290.         /* Try to use what we have */
  1291.         bp = NULLBUF;
  1292.         while((tbp = dequeue(&re->data)) != NULLBUF){
  1293.         ntohrspf(&rspf,&tbp);
  1294.         if(rspf.pkthdr.fragn != (last+1)){  /* a missing fragment */
  1295.             if(bp != NULLBUF)
  1296.             update_in(bp,re->addr);
  1297.             bp = NULLBUF;
  1298.             if(rspf.pkthdr.sync != 0)
  1299.             pullup(&tbp,NULLCHAR,rspf.pkthdr.sync - 4);
  1300.             else {    /* no sync possible in this fragment */
  1301.              free_p(tbp);
  1302.              continue;
  1303.             }
  1304.         }
  1305.         append(&bp,tbp);
  1306.         last = rspf.pkthdr.fragn;
  1307.         }
  1308.         if(bp != NULLBUF)
  1309.         update_in(bp,re->addr);
  1310.         del_reasm(re);
  1311.         re = Rspfreasmq;    /* start over again */
  1312.         }
  1313.     else
  1314.          re = re->next;
  1315.     /* Find the oldest fragment and restart the reassembly timer */
  1316.     low = 0;
  1317.     for(re = Rspfreasmq; re != NULLRREASM; re = re->next)
  1318.     if(re->time < low || low == 0)
  1319.         low = re->time;
  1320.     if(low){
  1321.     Rspfreasmt.start = RSPF_RTIME*1000/MSPTICK - Clock + low;
  1322.     if(Rspfreasmt.start > 0) /* just to be safe */
  1323.         start_timer(&Rspfreasmt);
  1324.     }
  1325. }
  1326.  
  1327. /* Handle incoming routing updates (a reassembled envelope) */
  1328. static void
  1329. update_in(bp,addr)
  1330. struct mbuf *bp;    /* Reassembled data packet, without packet header */
  1331. int32 addr;        /* Senders address (in host order) */
  1332. {
  1333.     struct rspfnodeh nodeh;
  1334.     struct rspflinkh linkh;
  1335.     struct rspfadj *adj;
  1336.     struct mbuf *tbp, *tmp, *b;
  1337.     int linkcnt = 0, adjcnt = 0;
  1338.     int16 offset = 0;
  1339.     tbp = b = NULLBUF;
  1340.     /* Check to see if the sender is an adjacency */
  1341.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  1342.      if(adj->addr == addr)
  1343.           break;
  1344.     /* One may argue that updates from non-adjacencies should not be
  1345.      * accepted since they will not contribute to the routing table.
  1346.      * But it might happen that the sender will very shortly become an
  1347.      * adjacency, and then its routing update will come handy. By increasing
  1348.      * Keeprouter, routing updates from disjoint routers will not be not be
  1349.      * purged when makeroutes() is called this time.
  1350.      */
  1351.     if(adj == NULLADJ) {
  1352.      ++Rspf_stat.noadjupdate;
  1353.      Keeprouter += 2;
  1354.     }
  1355.     else
  1356.     psignal(adj,1);        /* alert anyone waiting for the update */
  1357.     while(offset < len_p(bp)){
  1358.     if(adjcnt){
  1359.         if(dup_p(&tmp,bp,offset,5) == 5){
  1360.         append(&tbp,tmp);
  1361.         offset += 5;
  1362.         adjcnt--;
  1363.         continue;
  1364.         }
  1365.         else break;
  1366.     }
  1367.     if(tbp != NULLBUF){
  1368.         tmp = htonrspflink(&linkh,tbp);
  1369.         append(&b,tmp);
  1370.         tbp = NULLBUF;
  1371.     }
  1372.     if(linkcnt){
  1373.         if(dup_p(&tbp,bp,offset,RSPFLINKLEN) == RSPFLINKLEN){
  1374.         ntohrspflink(&linkh,&tbp);
  1375.         adjcnt = linkh.adjn;
  1376.         offset += RSPFLINKLEN;
  1377.         linkcnt--;
  1378.         continue;
  1379.         }
  1380.         else
  1381.         break;
  1382.     }
  1383.     if(b != NULLBUF){
  1384.         tmp = htonrspfnode(&nodeh,b);
  1385.         node_in(tmp,addr,1);     /* a full router update */
  1386.         b = NULLBUF;
  1387.     }
  1388.     if(dup_p(&tmp,bp,offset,RSPFNODELEN) == RSPFNODELEN){
  1389.         ntohrspfnode(&nodeh,&tmp);
  1390.         linkcnt = nodeh.links;
  1391.         offset += RSPFNODELEN;
  1392.     }
  1393.     else {
  1394.          free_p(bp);
  1395.          free_p(tmp);
  1396.          return;
  1397.     }
  1398.     }
  1399.     if(tbp != NULLBUF){
  1400.     /* adjust the adjacency counter in the link header */
  1401.     linkh.adjn -= adjcnt;
  1402.     tmp = htonrspflink(&linkh,tbp);
  1403.     append(&b,tmp);
  1404.     }
  1405.     /* adjust the link counter in the node header */
  1406.     nodeh.links -= linkcnt;
  1407.     tmp = htonrspfnode(&nodeh,b);
  1408.     free_p(bp);
  1409.     if(linkcnt || adjcnt)
  1410.     node_in(tmp,addr,0);     /* a partial router update */
  1411.     else
  1412.     node_in(tmp,addr,1);
  1413. }
  1414.     
  1415. static void
  1416. node_in(bp,addr,full)
  1417. struct mbuf *bp;    /* A single update, starting with the node header */
  1418. int32 addr;        /* Address of station we got packet from */
  1419. int full;        /* False if we got a partial update */
  1420. {
  1421.     struct mbuf *tbp;
  1422.     struct rspfnodeh nodeh, rnodeh;
  1423.     struct rspfrouter *rr;
  1424.     if(dup_p(&tbp,bp,0,RSPFNODELEN) != RSPFNODELEN) {
  1425.      free_p(bp);
  1426.      free_p(tbp);
  1427.      return;
  1428.     }
  1429.     ntohrspfnode(&nodeh,&tbp);
  1430.     if(ismyaddr(nodeh.addr)){
  1431.     /* If another station thinks our routing update sequence number is
  1432.      * larger than it really is, this might be because we have had
  1433.      * a fast system reset and routing updates from the old "epoch"
  1434.      * are still present in the network.
  1435.      */
  1436.     if(isnewer(nodeh.seq,Rspfseq)) {
  1437.         Rspfseq = nodeh.seq + 1;              /* Catch up */
  1438.         if(nodeh.seq == 32768 - 2 || nodeh.seq == -32768 + 1)
  1439.          Rspfseq = 0;                /* Wrap around */
  1440.         sendonerspf(nodeh.addr,addr); /* Send him the latest packet */
  1441.     }
  1442.     free_p(bp);         /* We already know our own adjacencies! */
  1443.     return;
  1444.     }
  1445.     if((rr = rr_lookup(nodeh.addr)) != NULLRROUTER) {
  1446.     if(dup_p(&tbp,rr->data,0,RSPFNODELEN) != RSPFNODELEN) {
  1447.         free_p(bp);
  1448.         free_p(tbp);
  1449.         return;
  1450.         }
  1451.     ntohrspfnode(&rnodeh,&tbp);
  1452.     if(nodeh.seq == rnodeh.seq && nodeh.subseq <= rr->subseq){
  1453.         free_p(bp);
  1454.         return;    /*  We already have this one */
  1455.     }
  1456.     if(isnewer(rnodeh.seq,nodeh.seq)){
  1457.         /* Send him the latest packet */
  1458.         sendonerspf(nodeh.addr,addr);
  1459.         free_p(bp);
  1460.         ++Rspf_stat.oldreport;
  1461.         return;
  1462.     }
  1463.     if(nodeh.subseq > rr->subseq && nodeh.seq == rnodeh.seq){
  1464.         rr->subseq = nodeh.subseq;
  1465.         partialupdate(rr,bp);
  1466.         makeroutes();
  1467.         return;
  1468.     }
  1469.     if(isnewer(nodeh.seq,rnodeh.seq) && !full){
  1470.         partialupdate(rr,bp);
  1471.         makeroutes();
  1472.         /* Send a "poll" packet */
  1473.         --nodeh.seq;
  1474.         nodeh.subseq = 0;
  1475.         nodeh.links = 0;
  1476.         tbp = htonrspfnode(&nodeh,NULLBUF);
  1477.         sendupdate(tbp,1,addr);
  1478.         ++Rspf_stat.outpolls;
  1479.         return;
  1480.     }
  1481.     }
  1482.     else {
  1483.     rr = (struct rspfrouter *) callocw(1,sizeof(struct rspfrouter));
  1484.     rr->next = Rspfrouters;
  1485.     Rspfrouters = rr;
  1486.     }
  1487.     free_p(rr->data);
  1488.     rr->data = bp;
  1489.     rr->subseq = nodeh.subseq;
  1490.     rr->time = Clock;
  1491.     rr->sent = 0;
  1492.     makeroutes();
  1493.     return;
  1494. }
  1495.  
  1496. /* Return the matching RSPF route entry for addr (in host order) */
  1497. static struct rspfrouter *
  1498. rr_lookup(addr)
  1499. int32 addr;
  1500. {
  1501.     struct rspfrouter *rr;
  1502.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next)
  1503.     /* this assumes that the first word of the header is the address */
  1504.     if(rr->data != NULLBUF && get32(rr->data->data) == addr)
  1505.         return rr;
  1506.     return NULLRROUTER;
  1507. }
  1508.  
  1509. /* Delete the route entry for address addr */
  1510. static void
  1511. rr_delete(addr)
  1512. int32 addr;
  1513. {
  1514.     struct rspfrouter *rr, *prev = NULLRROUTER;
  1515.     for(rr = Rspfrouters; rr != NULLRROUTER; prev = rr, rr = rr->next)
  1516.     /* this assumes that the first word of the header is the address */
  1517.     if(rr->data != NULLBUF && get32(rr->data->data) == addr) {
  1518.          if(prev != NULLRROUTER)
  1519.           prev->next = rr->next;
  1520.          else
  1521.           Rspfrouters = rr->next;
  1522.          free_p(rr->data);
  1523.          free((char *)rr);
  1524.          return;
  1525.     }
  1526. }
  1527.  
  1528. /* Handle incoming partial routing updates. Adjacencies from bp will be
  1529.  * merged into rr->data
  1530.  */
  1531. static void
  1532. partialupdate(rr,bp)
  1533. struct rspfrouter *rr;     /* current node entry in routing table */
  1534. struct mbuf *bp;    /* data packet, starting with node header */
  1535. {
  1536.     struct rspflinkh linkh, rlinkh;
  1537.     struct rspfnodeh rnodeh;
  1538.     struct mbuf *wbp, *tbp, *tmp, *b;
  1539.     int adjcnt = 0, radjcnt, rlinkcnt = 0, bits, rbits, added = 0;
  1540.     int32 addr, raddr;
  1541.  
  1542.     rlinkh.adjn = 0;
  1543.     rr->time = Clock;
  1544.     rr->sent = 0;
  1545.     /* Make a working copy of the stored routing update */
  1546.     if(dup_p(&wbp,rr->data,0,len_p(rr->data)) != len_p(rr->data)) {
  1547.      free_p(wbp);
  1548.      free_p(bp);
  1549.      return;
  1550.     }
  1551.     ntohrspfnode(&rnodeh,&wbp);
  1552.     pullup(&bp,NULLCHAR,RSPFNODELEN);    /* Strip off the node header */
  1553.     while(len_p(bp) > 0)
  1554.     if(adjcnt == 0) {
  1555.          if(ntohrspflink(&linkh,&bp) == -1) {
  1556.           free_p(wbp);
  1557.           free_p(bp);
  1558.           return;
  1559.          }
  1560.          adjcnt = linkh.adjn;
  1561.     }
  1562.     else {
  1563.         bits = PULLCHAR(&bp);    /* get one adjacency to merge */
  1564.         if(pullup(&bp,(char *)&addr,4) != 4) {
  1565.          free_p(wbp);
  1566.          free_p(bp);
  1567.          return;
  1568.         }
  1569.         addr = get32((char *)&addr); /* convert to host order */
  1570.         --adjcnt;
  1571.         radjcnt = 0;
  1572.         b = tbp = NULLBUF;
  1573.         for(;;) {            /* search through stored update */
  1574.         if(radjcnt == 0 || len_p(wbp) == 0) {
  1575.              if(tbp != NULLBUF){
  1576.               rlinkh.adjn -= radjcnt;
  1577.               tmp = htonrspflink(&rlinkh,tbp);
  1578.               ++rlinkcnt;
  1579.               append(&b,tmp);
  1580.               tbp = NULLBUF;
  1581.              }
  1582.              if(len_p(wbp) == 0)
  1583.               break;
  1584.         }
  1585.         if(radjcnt == 0) {
  1586.              ntohrspflink(&rlinkh,&wbp);
  1587.              radjcnt = rlinkh.adjn;
  1588.              if(rlinkh.horizon == linkh.horizon && rlinkh.cost ==
  1589.             linkh.cost && rlinkh.erp == linkh.erp) {
  1590.               pushadj(&tbp,addr,bits);
  1591.               ++rlinkh.adjn;
  1592.               added = 1;
  1593.              }
  1594.         }
  1595.         else {
  1596.             rbits = PULLCHAR(&wbp);
  1597.             raddr = pull32(&wbp);
  1598.             --radjcnt;
  1599.             if(bits != rbits || addr != raddr)
  1600.             pushadj(&tbp,raddr,rbits);    /* Put it back */
  1601.             else
  1602.              --rlinkh.adjn;    /* deleted one adjacency */
  1603.             }
  1604.         }
  1605.         wbp = b;    /* wbp now keeps link headers and adjacencies */
  1606.         if(linkh.cost < 255 && !added){ /* Append the new link */
  1607.         ++rnodeh.links;         /* We will add one extra link */
  1608.         tmp = alloc_mbuf(5);
  1609.         *tmp->data = bits;
  1610.         put32(tmp->data+1,addr);
  1611.         tmp->cnt = tmp->size;
  1612.         tbp = htonrspflink(&linkh,tmp);
  1613.         append(&wbp,tbp);
  1614.         }
  1615.         added = 0;
  1616.         }
  1617.    free_p(rr->data);
  1618.    rnodeh.links = rlinkcnt;
  1619.    rr->data = htonrspfnode(&rnodeh,wbp);
  1620. }
  1621.  
  1622. /* The shortest path first algorithm */
  1623. static void
  1624. makeroutes()
  1625. {
  1626.     int bits, i, low, adjcnt;
  1627.     struct mbuf *bp;
  1628.     struct route *rp, *rp2, *saved[NROUTE];
  1629.     struct rspfadj *adj, *lowadj, *gateway;
  1630.     char *lowp, *r;
  1631.     int32 lowaddr;
  1632.     struct rspflinkh linkh;
  1633.     struct rspfrouter *rr, *rrprev;
  1634.     if(Keeprouter)    /* if false, purge unreachable router entries */
  1635.      --Keeprouter;
  1636.     /* Before we change anything in the routing table, we have to save
  1637.      * each local adjacencies riface pointer
  1638.      */
  1639.     for(adj = Adjs; adj != NULLADJ; adj = adj->next)
  1640.     adj->scratch = (void *) rtif_lookup(adj->addr);
  1641.     /* Remove all non-manual routes */
  1642.     for(bits = 1; bits <= 32; bits++)
  1643.     for(i = 0; i < NROUTE; i++)
  1644.         for(rp = Routes[bits-1][i]; rp != NULLROUTE; rp = rp->next)
  1645.         if(dur_timer(&rp->timer) != 0)
  1646.             rt_drop(rp->target,bits);
  1647.     if((rp = rt_blookup(0,0)) != NULLROUTE && dur_timer(&rp->timer) != 0)
  1648.      rt_drop(0,0);    /* delete non-manual default route */
  1649.     /* Temporarily remove all 32-bit specific manual routes. This will make
  1650.      * it possible to prevent loops in findlowroute()
  1651.      */
  1652.     for(i = 0; i < NROUTE; i++) {
  1653.     saved[i] = Routes[31][i];
  1654.     Routes[31][i] = NULLROUTE;
  1655.     }
  1656.     for(;;) {
  1657.     lowadj = NULLADJ;
  1658.     lowp = NULLCHAR;
  1659.     low = 255;
  1660.     for(adj = Adjs; adj != NULLADJ; adj = adj->next){
  1661.        if(adj->scratch == NULL)
  1662.         continue;        /* skip unreachable adjacency */
  1663.         if(!adj->added){
  1664.         if(adj->cost <= low){
  1665.             low = adj->cost;
  1666.             lowadj = adj;
  1667.             lowp = NULLCHAR;
  1668.         }
  1669.         }
  1670.         else
  1671.         if((r = findlowroute(adj->addr,&low,adj->cost,&lowaddr,&bits))
  1672.           != NULLCHAR) {
  1673.             lowp = r;
  1674.             gateway = adj;
  1675.             lowadj = NULLADJ;
  1676.             }
  1677.     }
  1678.     if(lowadj != NULLADJ){
  1679.         lowadj->added++;
  1680.         rp = rt_add(lowadj->addr,32,0,
  1681.            ((struct rspfiface *)lowadj->scratch)->iface,
  1682.            (int32)lowadj->cost,0,0);
  1683.         /* set the timer to a dummy value. This makes it possible to
  1684.          * distinguish between manual and RSPF-generated routes.
  1685.          * The timer is never started however since RSPF handles the
  1686.          * expiration of routes itself.
  1687.          */
  1688.         set_timer(&rp->timer,MSPTICK);
  1689.         continue;
  1690.     }
  1691.     if(lowp != NULLCHAR){
  1692.         /* Check if we already have this one */
  1693.         rp = rt_blookup(lowaddr,bits);
  1694.         if((rp == NULLROUTE || (rp != NULLROUTE && 
  1695.             rp->metric > (int32) low)) && !ismyaddr(lowaddr)) {
  1696.          /* This is a new route, or a route with strict lower cost than
  1697.           * the previous route (possible only if it was manual)
  1698.           */
  1699.         rp = rt_add(lowaddr,bits,gateway->addr,
  1700.                ((struct rspfiface *)gateway->scratch)->iface,
  1701.                (int32)low,0,0);
  1702.         set_timer(&rp->timer,MSPTICK); /* a dummy value */
  1703.         }
  1704.         *lowp |= 128; /* mark the route as added in any case */
  1705.     }
  1706.     else
  1707.         break; /* no more routes */
  1708.     }
  1709.     /* Add the saved 32 bit routes, if there isn't now a better route */
  1710.     for(i = 0; i < NROUTE; i++) {
  1711.     rp = saved[i];
  1712.     while(rp != NULLROUTE) {
  1713.          rp2 = rt_blookup(rp->target,32);
  1714.          if(rp2 == NULLROUTE || (rp2 != NULLROUTE && rp2->metric >= rp->metric)) {
  1715.           rp2 = rt_add(rp->target,32,rp->gateway,rp->iface,rp->metric,
  1716.                    0,rp->flags & RTPRIVATE);
  1717.           rp2->uses = rp->uses;
  1718.          }
  1719.          rp2 = rp->next;
  1720.          free((char *)rp);
  1721.          rp = rp2;
  1722.     }
  1723.     }
  1724.     /* Reset all flags */
  1725.     for(adj = Adjs; adj != NULLADJ; adj = adj->next) {
  1726.     adj->added = 0;
  1727.     adj->scratch = NULL;
  1728.     }
  1729.     for(rr = Rspfrouters; rr != NULLRROUTER; rr = rr->next){
  1730.     i = len_p(rr->data) - RSPFNODELEN;
  1731.     /* jump past the node header */
  1732.     if(dup_p(&bp,rr->data,RSPFNODELEN,i) != i) {
  1733.          free_p(bp);
  1734.          continue;
  1735.     }
  1736.     adjcnt = 0;
  1737.     while(i > 0){
  1738.         if(adjcnt){
  1739.         if(!Keeprouter && (*bp->data & 128) == 0) {
  1740.              /* This router has an adjacency that was not added. That
  1741.               * means that the router is unreachable. Mark the
  1742.               * stored routing update for deletion.
  1743.               */
  1744.              free_p(bp);
  1745.              free_p(rr->data);
  1746.              rr->data = NULLBUF;    /* indicates disposal */
  1747.              break;
  1748.         }
  1749.         *bp->data &= ~128;    /* Clear the "added" bit */
  1750.         pullup(&bp,NULLCHAR,5);
  1751.         i -= 5;
  1752.         --adjcnt;
  1753.         continue;
  1754.         }
  1755.         ntohrspflink(&linkh,&bp);
  1756.         adjcnt = linkh.adjn;
  1757.         i -= RSPFLINKLEN;
  1758.         }
  1759.     }
  1760.     if(Keeprouter)    /* nothing more to do */
  1761.      return;
  1762.     /* Delete any routers that were discovered as being unreachable */
  1763.     rrprev = NULLRROUTER;
  1764.     rr = Rspfrouters;
  1765.     for(;;) {
  1766.      for(rrprev = NULLRROUTER, rr = Rspfrouters; rr != NULLRROUTER;
  1767.          rrprev = rr, rr = rr->next)
  1768.           if(rr->data == NULLBUF) {
  1769.            if(rrprev != NULLRROUTER)
  1770.             rrprev->next = rr->next;
  1771.            else
  1772.             Rspfrouters = rr->next;
  1773.            free((char *)rr);
  1774.            break;
  1775.           }
  1776.      if(rr == NULLRROUTER)
  1777.           break;
  1778.     }
  1779. }
  1780.  
  1781. /* Find a route from addr with the lowest cost. Returns a pointer to the
  1782.  * buffer that keeps the significant bit count of the selected route.
  1783.  */
  1784. static char *
  1785. findlowroute(addr,low,add,resaddr,resbits)
  1786. int32 addr;            /* The node whose routes will be examined */
  1787. int *low;            /* Lowest cost so far */
  1788. int add;            /* Cost of this node */
  1789. int32 *resaddr;            /* Resulting route stored here */
  1790. int *resbits;            /* Significant bits of resulting route */
  1791. {
  1792.     struct mbuf *bp;
  1793.     struct rspfrouter *rr;
  1794.     struct rspflinkh linkh;
  1795.     struct route *rp;
  1796.     char *r, *retval = NULLCHAR;
  1797.     int adjcnt = 0;
  1798.  
  1799.     linkh.cost = 0;
  1800.     if((rr = rr_lookup(addr)) == NULLRROUTER)
  1801.     return NULLCHAR;
  1802.     if((rp = rt_blookup(addr,32)) != NULLROUTE && rp->metric < add)
  1803.      return NULLCHAR;    /* already added this one, no loops thanks */
  1804.     if(dup_p(&bp,rr->data,RSPFNODELEN,len_p(rr->data) - RSPFNODELEN) !=
  1805.        len_p(rr->data) - RSPFNODELEN) {
  1806.      free_p(bp);
  1807.      return NULLCHAR;
  1808.     }
  1809.     while(len_p(bp) > 0){
  1810.     if(adjcnt){
  1811.         if(*bp->data & 128) {
  1812.         (void) PULLCHAR(&bp);
  1813.         if((r = findlowroute(pull32(&bp),low,add+linkh.cost,resaddr,
  1814.           resbits)) != NULLCHAR)
  1815.              retval = r;
  1816.         }
  1817.         else {
  1818.         *low = add + linkh.cost;
  1819.         retval = bp->data;
  1820.         *resbits = PULLCHAR(&bp);
  1821.         *resaddr = pull32(&bp);
  1822.         pullup(&bp,NULLCHAR,5*(adjcnt-1));
  1823.         adjcnt = 1;    /* No need to check the rest of this link */
  1824.         }
  1825.         --adjcnt;
  1826.         continue;
  1827.     }
  1828.     ntohrspflink(&linkh,&bp);
  1829.     if((add + linkh.cost) <= *low)
  1830.         adjcnt = linkh.adjn;
  1831.     else
  1832.         pullup(&bp,NULLCHAR,5*linkh.adjn);
  1833.     }
  1834.     return retval;
  1835. }
  1836.  
  1837. #ifndef    AX25
  1838. /*
  1839.  * Dummy stub for RSPF if AX25 is not configured.
  1840.  */
  1841. static struct lq *
  1842. al_lookup(ifp,addr)
  1843. struct iface *ifp;
  1844. char *addr;
  1845. {
  1846.     return NULLLQ;
  1847. }
  1848. #endif    /* AX25 */
  1849.